package com.itany.corejava.code07_常用类.string;

/**
 * @author MissChen
 * @date 2024年03月01日 9:58
 */
public class Test14_性能比较 {
    public static void main(String[] args) {
        testString();
        testStringBuffer();
        testStringBuilder();
    }
    public static void testString(){
        long begin=System.currentTimeMillis();
        String s="";
        for(int i=0;i<10000;i++){
            s+="hello";
        }
        long end=System.currentTimeMillis();
        System.out.println("String操作1w次耗费了"+(end-begin));

    }

    public static void testStringBuffer(){
        long begin=System.currentTimeMillis();
        StringBuffer buffer=new StringBuffer();
        for(int i=0;i<10000;i++){
            buffer.append("hello");
        }
        long end=System.currentTimeMillis();
        System.out.println("StringBuffer操作1w次耗费了"+(end-begin));
    }

    public static void testStringBuilder(){
        long begin=System.currentTimeMillis();
        StringBuilder builder=new StringBuilder();
        for(int i=0;i<10000;i++){
            builder.append("hello");
        }
        long end=System.currentTimeMillis();
        System.out.println("StringBuilder操作1w次耗费了"+(end-begin));
    }
}
